import memcache
from memcacheq import MemcacheQueue
import sys, time

mq = MemcacheQueue('perftest', 
                   memcache.Client(['127.0.0.1:11211']))

i_am = 'writer'
if len(sys.argv) > 1 and sys.argv[1] == '-r':
    i_am = 'reader'

COUNT = 1000
start =  time.time()
if i_am == 'writer':
    for x in range(COUNT):
        mq.add('message %s' % x)
    print 'Added', COUNT
elif i_am == 'reader':
    idx = 0
    retries = 0
    GO = True
    while GO:
        m = mq.get()
        if m:
            idx += 1
        else:
            retries += 1
        if retries > 1000: GO = False
    print 'Read', idx
end = time.time()
print '%s took %s seconds' % (i_am, end-start)
